//
//  YLIntegralMallViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/5.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLIntegralMallViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "YLMallGoodModel.h"
#import <YYModel.h>
#import "WHProgressHUD.h"
#import "YLMallTitleModel.h"
#import "YLMallItemModel.h"
@interface YLIntegralMallViewModel()
@property (nonatomic, strong) NSMutableArray *goods;

@property (nonatomic, strong) NSMutableArray *mallTitles;

@property (nonatomic, strong) NSMutableArray *mallItems;



@end

@implementation YLIntegralMallViewModel

// 我的积分和兑换
- (NSMutableArray *)mallTitles
{
    if (!_mallTitles) {
        _mallTitles = [NSMutableArray arrayWithCapacity:2];
        
        YLMallTitleModel *model = nil;
            
        model = [YLMallTitleModel modelWithIcon:@"Integral" title:@"我的积分" hiddenLine:YES  content:@"0" clickBlock:^NSString *{
                return @"YLIntegralController";
            }];
            [_mallTitles addObject:model];
            
            model = [YLMallTitleModel modelWithIcon:@"Integral" title:@"我的兑换" hiddenLine:NO  content:@"查看" clickBlock:^NSString *{
                return @"YLExchangeRecordController";
            }];
            
        [_mallTitles addObject:model];

        
    }
    return _mallTitles;
}

- (NSMutableArray *)mallItems
{
    if (!_mallItems) {
        _mallItems = [NSMutableArray arrayWithCapacity:2];
        YLMallItemModel *model = nil;
        model = [YLMallItemModel modelWithIcon:@"shake" title:@"摇一摇" clickBlock:^NSString *{
            [WHProgressHUD showError:@"敬请期待"];
            
            
            return nil;
        }];
        [_mallItems addObject:model];
        
        model = [YLMallItemModel modelWithIcon:@"limitedTimeExchange" title:@"限时兑换" clickBlock:^NSString *{
            [WHProgressHUD showError:@"敬请期待"];
            return nil;
        }];
        [_mallItems addObject:model];
    
    }
    return _mallItems;

}



- (RACCommand *)productListCommand
{
    if (!_productListCommand) {
        __weak typeof(self) weakSelf = self;
        _productListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@Point/PGetPointProductList", YLURL];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                params[@"PageIndex"] = input;
                NSInteger count = 6;
                params[@"MaxResultCount"] = @(count);
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMallGoodGroup *group = [YLMallGoodGroup yy_modelWithDictionary:responseObject[@"httpResult"]];
                        NSMutableArray *goods =  weakSelf.groups.lastObject;
                        
                        
                        YLMallTitleModel *model = weakSelf.mallTitles.firstObject;
                        model.content = group.pointQuota;
                        weakSelf.totalPoint = group.pointQuota;
                        
                        if ([input integerValue] == 1) {
                            [goods removeAllObjects];
                        }
                        
                        [goods addObjectsFromArray:group.item];
                        [subscriber sendNext:nil];
                        
                        // 总数 < 列表数
                        if (!(group.count > goods.count)) {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    [subscriber sendCompleted];
            
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _productListCommand;
}

- (NSMutableArray *)groups
{
    if (!_groups) {
        _groups = [NSMutableArray arrayWithCapacity:3];
        [_groups addObject:self.mallTitles];
        [_groups addObject:self.mallItems];
        [_groups addObject:self.goods];
    }
    return _groups;
}

- (NSMutableArray *)goods
{
    if (!_goods) {
        _goods = [NSMutableArray array];
    }
    return _goods;
}


@end
